cd "C:\Users\ntdavis2\Dropbox\SCOTUS Legitimacy and Democratic Values\RAPID Data\figures"

* use: scd-panel-raw.dta

********************************************************************************
* piped PID: recollected at wave 4 for experiment
********************************************************************************

gen wave4pid = pid7_w3r
recode wave4pid 1/3=1 4=2 5/7=3 8/999=2
/// 4 = other, 5 = not sure --> both recoded to independent category
label define wave4pid 1 "Democrat" 3 "Republican" 2 "Independent"
label value wave4pid wave4pid

gen pid2 = 0 if wave4pid==1
replace pid2 = 1 if wave4pid==3

gen pid_3 = wave4pid

********************************************************************************
* demographics 
********************************************************************************

* race

gen race_cat = race
recode race_cat 1=1 2=2 3=3 4/999=4
label define race_cat 1 "White" 2 "Black" 3 "Hispanic" 4 "Other"
label value race_cat race_cat

gen white = (race_cat==1)

* gender = gender

* age

gen age = 2022-birthyr

* educ = educ

* pid = wave4pid /// pure independents = middle category
gen pid_3 = wave4pid

* ideo
gen ideo = ideo5
recode ideo 1/2=2 3=3 4/5=4 5/999=1

********************************************************************************
* court attitudes  
********************************************************************************

* judicial cynicism 
factor q6_w2_1 q6_w2_2 q6_w2_3 q6_w2_4 q6_w2_5
predict cynicism

* diffuse support for the court
factor q4_w2_1 q4_w2_2 q4_w2_3 q4_w2_4 q4_w2_5 q4_w2_6
predict diffuse

* democratic values
factor q10_1_w2 q10_2_w2 q10_3_w2 q10_4_w2 q10_5_w2 q10_6_w2 q10_7_w2
predict values

* approval 
gen approval = q4_w3r
recode approval 1=5 2=4 3=3 4=2 5=1

* perceived ideology
gen courtideo = q5_w2

* normalizing 
foreach x in cynicism diffuse values age educ wave4pid approval courtideo { 
    sum `x'  
	gen `x'01 = (`x'-r(min))/(r(max)-r(min)) 
	}

********************************************************************************
* experimental outcomes
********************************************************************************

* candidate strength 
rename q11_w3r candstr
label var candstr "Perceived candidate strength"

* donation amount
gen donation = q10_w3r
gen donation_cat = donation
recode donation_cat 0=0 1/100=1 101/500=2 501/3000=3

* perceived ideology 
rename q8_w3r candideo
recode candideo 8/9999=.
label var candideo "Perceived candidate ideology"

* affective rating
gen candtherm = q12_sam_w3r
recode candtherm 101/9999=. 
label var candtherm "Candidate favorability"

* treatment group 
rename treatment_w3r treatment
label var treatment "Experimental condition"

********************************************************************************
* candidate issue priorities
********************************************************************************

gen abortionrank = . 
replace abortionrank = 1 if q1_ranking_1_w3r == 1
replace abortionrank = 2 if q1_ranking_2_w3r == 1
replace abortionrank = 3 if q1_ranking_3_w3r == 1
replace abortionrank = 4 if q1_ranking_4_w3r == 1
replace abortionrank = 5 if q1_ranking_5_w3r == 1
replace abortionrank = 6 if q1_ranking_6_w3r == 1
replace abortionrank = 7 if q1_ranking_7_w3r == 1

label var abortionrank "Abortion"
egen stdabortion = std(abortionrank)

gen scrank = . 
replace scrank = 1 if q1_ranking_1_w3r == 2
replace scrank = 2 if q1_ranking_2_w3r == 2
replace scrank = 3 if q1_ranking_3_w3r == 2
replace scrank = 4 if q1_ranking_4_w3r == 2
replace scrank = 5 if q1_ranking_5_w3r == 2
replace scrank = 6 if q1_ranking_6_w3r == 2
replace scrank = 7 if q1_ranking_7_w3r == 2

label var scrank "Supreme Court"
egen stdsc = std(scrank)

gen taxesrank = . 
replace taxesrank = 1 if q1_ranking_1_w3r == 3
replace taxesrank = 2 if q1_ranking_2_w3r == 3
replace taxesrank = 3 if q1_ranking_3_w3r == 3
replace taxesrank = 4 if q1_ranking_4_w3r == 3
replace taxesrank = 5 if q1_ranking_5_w3r == 3
replace taxesrank = 6 if q1_ranking_6_w3r == 3
replace taxesrank = 7 if q1_ranking_7_w3r == 3

label var taxesrank "Taxes"
egen stdtaxes = std(taxesrank)

gen hcrank = . 
replace hcrank = 1 if q1_ranking_1_w3r == 4
replace hcrank = 2 if q1_ranking_2_w3r == 4
replace hcrank = 3 if q1_ranking_3_w3r == 4
replace hcrank = 4 if q1_ranking_4_w3r == 4
replace hcrank = 5 if q1_ranking_5_w3r == 4
replace hcrank = 6 if q1_ranking_6_w3r == 4
replace hcrank = 7 if q1_ranking_7_w3r == 4

label var hcrank "Healthcare"
egen stdhc = std(hcrank)

gen fprank = . 
replace fprank = 1 if q1_ranking_1_w3r == 5
replace fprank = 2 if q1_ranking_2_w3r == 5
replace fprank = 3 if q1_ranking_3_w3r == 5
replace fprank = 4 if q1_ranking_4_w3r == 5
replace fprank = 5 if q1_ranking_5_w3r == 5
replace fprank = 6 if q1_ranking_6_w3r == 5
replace fprank = 7 if q1_ranking_7_w3r == 5

label var fprank "Foreign policy"
egen stdfp = std(fprank)

gen educrank = . 
replace educrank = 1 if q1_ranking_1_w3r == 6
replace educrank = 2 if q1_ranking_2_w3r == 6
replace educrank = 3 if q1_ranking_3_w3r == 6
replace educrank = 4 if q1_ranking_4_w3r == 6
replace educrank = 5 if q1_ranking_5_w3r == 6
replace educrank = 6 if q1_ranking_6_w3r == 6
replace educrank = 7 if q1_ranking_7_w3r == 6

label var educrank "Education"
egen stdeduc = std(educrank)

gen environrank = . 
replace environrank = 1 if q1_ranking_1_w3r == 7
replace environrank = 2 if q1_ranking_2_w3r == 7
replace environrank = 3 if q1_ranking_3_w3r == 7
replace environrank = 4 if q1_ranking_4_w3r == 7
replace environrank = 5 if q1_ranking_5_w3r == 7
replace environrank = 6 if q1_ranking_6_w3r == 7
replace environrank = 7 if q1_ranking_7_w3r == 7

label var environrank "Environment"
egen stdenviron = std(environrank)

* distribution of top-ranked issues
tab q1_ranking_1_w3r

* normalized cumulative importance
* flipping scoring so low values (higher ranks) can be graphed intuitively
omscore hcrank
omscore taxesrank 
omscore educrank
omscore abortionrank
omscore environrank
omscore fprank
omscore scrank

label var rr_abortionrank "Abortion"
label var rr_scrank "Supreme Court"
label var rr_taxesrank "Taxes"
label var rr_hcrank "Healthcare"
label var rr_fprank "Foreign policy"
label var rr_educrank "Education"
label var rr_environrank "Environment"

********************************************************************************
* removing missing
********************************************************************************

gen nomiss = 1
replace nomiss = . if pid2==.
replace nomiss = . if diffuse01==. & pid2==.

********************************************************************************
* figure 1
********************************************************************************

mean rr_hcrank rr_taxesrank rr_educrank rr_abortionrank rr_environrank rr_fprank rr_scrank
est store ranks

coefplot ranks ||, msymbol(O) mcolor(black) mfcolor(black) msize(med) ciopts(lcolor(black))  ///
xlab( 5 "3.0" 4.5 "3.5" 4 "4.0" 3.5 "4.5" 3 "5.0",grid glcolor(gs14) glstyle(solid)  gmax gmin tlength(0)) ///
ylab(, grid glcolor(gs15) glstyle(solid) gmax gmin tlength(0)) ///
xsc(lcolor(none) r()) ysc(lcolor(none)) /// 
xtitle("Weighted importance") ytitle(" ", orientation(hor)) scale(1.2) aspect(1) ///
plotregion(lcolor(none))

********************************************************************************
* figure 2
********************************************************************************

reg scrank cynicism01 diffuse01 courtideo01 approval01 i.race_cat educ01 age01 i.ideo b1.pid_3 [pweight=weight]
est store scrank

coefplot (scrank),  msym(O) mcolor(black) mfcolor(white) mlwidth(medium) ciopts(lcolor(black)) ///
xlab(, grid glstyle(solid) glcolor(gs14) gmax gmin tlength(0) ) ///
ylab(1 "Judicial cynicism" 2 "Diffuse support" 3 "Perceived Court ideology" 4 "Court disapproval" 5 "Black" 6 "Hispanic" 7 "Other" 8 "Education" 9 "Age" 10 "Liberal" 11 "Moderate" 12 "Conservative" 13 "Indpendent" 14 "Republican", grid glstyle(solid) glcolor(gs14) gmax gmin tlength(0) ) ///
xsc(lcolor(none) r() ) ysc(lcolor(none) )  /// 
title(" ") xtitle("Marginal effect on rank") ytitle( , orientation(hor)) drop(_cons) plotregion(color(white) lcolor(none)) graphregion(color(white)) xline(0, lcolor(red) lstyle(dot) lwidth(thick)) ///
aspect(1) scale(1.2) saving(scranks_all.gph, replace)

********************************************************************************
* figure 3
********************************************************************************

mean candideo if pid2==0 [pweight=weight_w3r], over(treatment)
est store dem
mean candideo if pid2==1 [pweight=weight_w3r], over(treatment)
est store rep

coefplot (dem, msym(O) mcolor(black) msize(medium) ciopts(lcolor(black))) || (rep, msym(D) mfcolor(white) mlcolor(black) msize(medium) ciopts(lcolor(black))), vertical msym(O) ///
xlab( 1 "No cue" 2 "Pro-reform" 3 "Anti-reform", grid glstyle(solid) glcolor(gs14) gmax gmin tlength(0) ) ///
ylab( 2 `""Somewhat liberal" "2""' 3 `""Slightly liberal" "3""' 4 `""Moderate" "4""' 5 `""Slightly conservative" "5""' 6 `""Somewhat conservative" "6""', grid glstyle(solid) glcolor(gs14) gmax gmin tlength(0) angle(0)) ///
xsc(lcolor(none) r() ) ysc(lcolor(none) ) legend(off)  /// 
title(" ") ytitle( , orientation(hor)) saving(strr.gph, replace)

gen combo = .
replace combo = 1 if pid2==0 & treatment==1
replace combo = 2 if pid2==0 & treatment==2
replace combo = 3 if pid2==0 & treatment==3

replace combo = 4 if pid2==1 & treatment==1
replace combo = 5 if pid2==1 & treatment==2
replace combo = 6 if pid2==1 & treatment==3

* testing differences between conditions in candidate ideology

reg candideo i.treatment##pid2
pwcompare i.treatment##pid2

* generating p-values for simple difference in means
* first dummy-out treatment X pid combinations
tab combo, gen(combo)

* OLS of candidate ideology, which permits significance tests found in image
reg candideo combo1-combo5
lincom combo1-combo2
lincom combo1-combo3
reg candideo combo2-combo6
lincom combo4-combo5
lincom combo4-combo6

********************************************************************************
* figure 4
********************************************************************************

* strength

mean candstr if pid2==0 [pweight=weight_w3r], over(treatment)
est store dem
mean candstr if pid2==1 [pweight=weight_w3r], over(treatment)
est store rep

coefplot (dem, msym(O) mcolor(black) msize(medium) ciopts(lcolor(black))) || (rep, msym(D) mfcolor(white) mlcolor(black) msize(medium) ciopts(lcolor(black))), vertical msym(O) ///
xlab( 1 "No cue" 2 "Pro-reform" 3 "Anti-reform", grid glstyle(solid) glcolor(gs14) gmax gmin tlength(0) ) ///
ylab( 2.25 2.5 "2.50" 2.75 3 "3.00" 3.25 3.5 "3.50", grid glstyle(solid) glcolor(gs14) gmax gmin tlength(0) angle(0)) ///
xsc(lcolor(none) r() ) ysc(lcolor(none) ) legend(off)  /// 
title(" ") ytitle( , orientation(hor)) saving(strr.gph, replace)

* testing differences between conditions in candidate strength

reg candstr i.treatment##pid2 [pweight=weight_w3r]
pwcompare i.treatment##pid2

* OLS of candidate ideology, which permits significance tests found in image
reg candstr combo1-combo5
lincom combo1-combo2
lincom combo1-combo3
reg candstr combo2-combo6
lincom combo4-combo5
lincom combo4-combo6

********************************************************************************
* figure 5
********************************************************************************

histogram diffuse, by(pid_3, note(" ") title(" ") col(3)) bin(30) percent plotregion(lcolor(none)) xsc(lcolor(none) r() ) ysc(lcolor(none) ) ytitle(" " ) xtitle(" ")  title(" ") ///
xlab(, grid glstyle(solid) glcolor(gs15) gmax gmin tlength(0)) ylab(0 "0%" 5 "5%" 10 "10%" 15 "15%", grid glstyle(solid) glcolor(gs15) gmax gmin tlength(0)) 

* mean differences
mean diffuse [pweight=weight_w3r], over(pid2)
reg diffuse i.pid2 [pweight=weight_w3r]
pwcompare i.pid2

********************************************************************************
* figure 6
********************************************************************************

reg candstr cynicism01 courtideo01 approval01 i.race_cat educ01 age01 i.ideo i.treatment##c.diffuse01 wave4pid [pweight=weight]
quietly margins, at(diffuse01=(0(.1)1) treatment=(2,3) )

marginsplot, title("All subjects") recast(line) recastci(rarea) xtitle("Diffuse support") ytitle("Candidate strength") ci1opts(color(blue%10)) ci2opts(color(red%50)) plot1opts(lc(blue%50)) plot2opts(lc(red%50)) ylab(2 "2.00" 2.25 "2.25" 2.5 "2.50" 2.75 "2.75" 3 "3.00" 3.25 "3.25" 3.5 "3.50" 3.75 "3.75" 4 "4.00", angle(0)) ///
addplot(hist diffuse01, legend(order(3 "Pro-reform" 4 "Anti-reform" 5 "Distribution of diffuse support") at(4)pos(0)) bcolor(gs8%20) blcolor(black%100) bin(10) percent yaxis(2) yscale(alt axis(2)) ytitle(" ", axis(2)) ylab(0 "0%" 10 "10%" 20 "20%" 30 " " 40 " " 50 " " 60 " " 70 " " 80 " " 90 " ", angle(0) axis(2))) saving(allsubjects, replace)


reg candstr cynicism01 courtideo01 approval01 i.race_cat educ01 age01 i.ideo i.treatment##c.diffuse01 [pweight=weight] if wave4pid==1
quietly margins, at(diffuse01=(0(.1)1) treatment=(2,3) )

marginsplot, title("Democrats") recast(line) recastci(rarea) xtitle("Diffuse support") ytitle("Candidate strength", axis(1)) ci1opts(color(blue%10)) ci2opts(color(red%50)) plot1opts(lc(blue%50) ) plot2opts(lc(red%50))  ylab(2 "2.00" 2.25 "2.25" 2.5 "2.50" 2.75 "2.75" 3 "3.00" 3.25 "3.25" 3.5 "3.50" 3.75 "3.75" 4 "4.00", angle(0)) ///
addplot(hist diffuse01 if wave4pid==1, legend(order(3 "Pro-reform" 4 "Anti-reform" 5 "Distribution of diffuse support") at(4)pos(0)) bcolor(gs8%20) blcolor(black%100) bin(10) percent yaxis(2) yscale(alt axis(2)) ytitle(" ", axis(2)) ylab(0 "0%" 10 "10%" 20 "20%" 30 " " 40 " " 50 " " 60 " " 70 " " 80 " " 90 " ", angle(0) axis(2))) saving(dems.gph, replace)

reg candstr cynicism01 courtideo01 approval01 i.race_cat educ01 age01 i.ideo i.treatment##c.diffuse01 [pweight=weight] if wave4pid==3
quietly margins, at(diffuse01=(0(.1)1) treatment=(2,3) )

marginsplot, title("Republicans") recast(line) recastci(rarea) xtitle("Diffuse support") ytitle("Candidate strength") ci1opts(color(blue%10)) ci2opts(color(red%50)) plot1opts(lc(blue%50)) plot2opts(lc(red%50))  ylab(2 "2.00" 2.25 "2.25" 2.5 "2.50" 2.75 "2.75" 3 "3.00" 3.25 "3.25" 3.5 "3.50" 3.75 "3.75" 4 "4.00", angle(0)) ///
addplot(hist diffuse01 if wave4pid==3, legend(order(3 "Pro-reform" 4 "Anti-reform" 5 "Distribution of diffuse support") at(4)pos(0)) bcolor(gs8%20) blcolor(black%100) bin(10) percent yaxis(2) yscale(alt axis(2)) ytitle(" ", axis(2)) ylab(0 "0%" 10 "10%" 20 "20%" 30 " " 40 " " 50 " " 60 " " 70 " " 80 " " 90 " ", angle(0) axis(2))) saving(rep.gph, replace) 

grc1leg allsubjects.gph dems.gph rep.gph,

********************************************************************************
* appendix
********************************************************************************

********************************************************************************
* figure A1
********************************************************************************

mean rr_hcrank rr_taxesrank rr_educrank rr_abortionrank rr_environrank rr_fprank rr_scrank if pid_3==1 [pweight=weight]
est store demranks

mean rr_hcrank rr_taxesrank rr_educrank rr_abortionrank rr_environrank rr_fprank rr_scrank if pid_3==3 [pweight=weight]
est store repranks

mean rr_hcrank rr_taxesrank rr_educrank rr_abortionrank rr_environrank rr_fprank rr_scrank [pweight=weight]
est store ranks

coefplot (ranks, mcolor(black) mfcolor(black) ciopts(lcolor(black))) (demranks, mcolor(blue) mfcolor(blue) ciopts(lcolor(blue))) (repranks, mcolor(red) mfcolor(red) ciopts(lcolor(red))) ||,   ///
xlab( 6 "2.0" 5.5 "2.5" 5 "3.0" 4.5 "3.5" 4 "4.0" 3.5 "4.5" 3 "5.0",grid glcolor(gs14) glstyle(solid)  gmax gmin tlength(0)) ///
ylab(, grid glcolor(gs15) glstyle(solid) gmax gmin tlength(0)) ///
xsc(lcolor(none) r()) ysc(lcolor(none)) legend(order(2 "All subjects" 4 "Democrats" 6 "Republicans")) /// 
xtitle("Weighted importance") ytitle(" ", orientation(hor)) scale(1.2) aspect(1) ///
plotregion(lcolor(none)) yline(1.5 2.5 3.5 4.5 5.5 6.5 7.5, lcolor(gs15) lstyle(solid))

********************************************************************************
* table A1
********************************************************************************

tab educ if pid_3!=.
tab gender if pid_3!=.
tab race if pid_3!=.
tab pid_3 
tab ideo5 if pid_3!=.

********************************************************************************
* table A2
********************************************************************************

corr candideo candstr candtherm
corr candideo candstr candtherm if wave4pid==1
corr candideo candstr candtherm if wave4pid==3
corr candideo candstr candtherm if wave4pid==2 & pol_group_w3r==1
corr candideo candstr candtherm if wave4pid==2 & pol_group_w3r==2

********************************************************************************
* table A3
********************************************************************************

factor q4_w2_1 q4_w2_2 q4_w2_3 q4_w2_4 q4_w2_5 q4_w2_6
alpha q4_w2_1 q4_w2_2 q4_w2_3 q4_w2_4 q4_w2_5 q4_w2_6

********************************************************************************
* table A4
********************************************************************************

* judicial cynicism 
factor q6_w2_1 q6_w2_2 q6_w2_3 q6_w2_4 q6_w2_5

********************************************************************************
* three-way interaction for cand support
********************************************************************************

reg candstr i.treatment##c.diffuse01##i.pid2 scrank white i.gender age ideo5 newsint faminc_new [pweight=weight] if nomiss==1
est store cand
estout cand, cells(b(star fmt(3)) se(par fmt(2))) nobaselevels stats(r2 N) unstack mlabels("Democrats" "Republicans") varlabels(2.treatment Pro-Reform 3.treatment Anti-Reform pid2 Republican diffuse01 Diffuse-support 2.treatment#c.diffuse01 "Diffuse support x Pro-Reform" 3.treatment#c.diffuse01 "Diffuse support x Anti-Reform" white White 2.gender Female age Age ideo5 "Lib-con placement" newsint "News interest" faminc_new "Family income" _cons Intercept) varwidth(30) 

reg candstr i.treatment##c.diffuse01##i.pid2 scrank white i.gender age ideo5 newsint faminc_new [pweight=weight] if nomiss==1
margins pid2, at(diffuse01=(0(.1)1) treatment=(2,3))
marginsplot, by(pid2)

********************************************************************************
* balance test, randomization check 
********************************************************************************

* no sig diffs
mlogit treatment pid2 diffuse01 white i.gender age ideo5 newsint faminc_new [pweight=weight] if nomiss==1

********************************************************************************
* testing partisan diffs in SC ranks with diffuse support X pid interaction 
********************************************************************************

reg scrank cynicism01 courtideo01 approval01 i.race_cat educ01 age01 i.ideo i.pid2##c.diffuse01 [pweight=weight]
quietly margins, at(diffuse01=(0(.1)1) pid2=(0,1))

marginsplot, title("All subjects") recast(line) recastci(rarea) xtitle("Diffuse support") ytitle("SC ranks") ci1opts(color(blue%10)) ci2opts(color(red%50)) plot1opts(lc(blue%50)) plot2opts(lc(red%50)) ylab( , angle(0)) ///
addplot(hist diffuse01, legend(order(1 "Democrats" 2 "Republicans") at(4)pos(0)) bcolor(gs8%20) blcolor(black%100) bin(10) percent yaxis(2) yscale(alt axis(2)) ytitle(" ", axis(2)) ylab(0 "0%" 10 "10%" 20 "20%" 30 " " 40 " " 50 " " 60 " " 70 " " 80 " " 90 " ", angle(0) axis(2))) saving(allsubjects, replace)

